import pycuda.driver as cuda                     # import PyCUDA module
import pycuda.autoinit, pycuda.compiler          # initialize the CUDA hardware
import numpy 

a = numpy.random.randn(4,4).astype(numpy.float32)# create a 4x4 array random numbers
a_gpu = cuda.mem_alloc(a.nbytes)                 # alloc the same sized array in GPU
cuda.memcpy_htod(a_gpu, a)                       # copy the data from host to device

mod = pycuda.compiler.SourceModule("""           # specify a CUDA kernel function
    __global__  void  twice (float *a)
    {
        int idx = threadIdx.x + threadIdx.y*4;
        a[idx] *= 2;
    }
    """)
func = mod.get_function("twice")                 # get the handle of CUDA kernel 
func(a_gpu, block=(4,4,1))              # run CUDA function with a configuration

a_doubled = numpy.empty_like(a)                  # create an empty buffer on host
cuda.memcpy_dtoh(a_doubled, a_gpu)               # copy result from device to host
print	a_double
print	a

